//
//  NewKuaiPayViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/3/5.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "NewKuaiPayViewController.h"
#import "JTCSM.h"
#import "RegisterVIew.h"
#import "NewTiXianFinishViewController.h"
#define KWidthOfRegister 50
static CGFloat space = 20;
@interface NewKuaiPayViewController ()<UITextFieldDelegate,UINavigationControllerDelegate>
{
    UILabel *banknameLabel;
    UILabel *bankcardLabel;
    UITextField *moneyTextfield;
    UILabel *bankname;
    UILabel *monyNum;
    NSInteger idx;
    NSArray *imageNameArray;
    NSArray *placeholderArray;
    RegisterVIew *registerV;
    UIButton *getKeyBtn;
    NSTimer *_timer1;
    NSTimer *_timer2;
    NSInteger count;
}
@end

@implementation NewKuaiPayViewController

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    [df isRemoveTarbar:YES];
    self.view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    // Do any additional setup after loading the view.
    [self layoutview];
    self.navigationController.delegate = self;
}
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)layoutview{
    CGFloat height = 90;
    UIView *backview = [[UIView alloc]initWithFrame:CGRectMake(0, KagainChangeHeight, KCONTROLLERWith-0, height)];
    backview.backgroundColor = [UIColor whiteColor];
    [df layerWithView:backview radius:4 width:0 color:nil];
    [self.view addSubview:backview];
    
    NSString *titleStr = @"A";
    float titlefontsize = 14;
    CGRect rect = [df labelHeightWithContent:titleStr height:30 font:titlefontsize];
    
    float moneyfontsize = 24;
    CGRect moneyrect = [df labelHeightWithContent:titleStr height:100 font:titlefontsize];
    
    float top  = (height-rect.size.height-moneyrect.size.height-13)/2;
    
    banknameLabel = [UILabel initWithFrame:CGRectMake(space,top, KSCREENWidth, rect.size.height) text:@"dfvdfvdfvdf" font:titlefontsize textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [backview addSubview:banknameLabel];
    
    bankcardLabel = [UILabel initWithFrame:CGRectMake(space, KVIEW_Y(banknameLabel)+13, KSCREENWidth, moneyrect.size.height+5) text:@"¥1234.00" font:moneyfontsize textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [backview addSubview:bankcardLabel];
    
    CGFloat bankheight = 50;
    UIView *bankView = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(backview)+15, KCONTROLLERWith, bankheight)];
    [self.view addSubview:bankView];
    bankView.backgroundColor = [UIColor whiteColor];
    bankView.userInteractionEnabled = YES;
    
    UILabel *banktitle = [UILabel initWithFrame:CGRectMake(space, 0, 80, bankheight) text:@"卡信息" font:14 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [bankView addSubview:banktitle];
    
    bankname = [UILabel initWithFrame:CGRectMake(KVIEW_X(banktitle)+space, 0, KCONTROLLERWith, bankheight) text:@"招商银行（8463）" font:14 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(153, 153, 153, 1) backgroundColor:nil];
    [bankView addSubview:bankname];
    
    CGFloat infoheight = 40;
    UIView *infoView = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(bankView), KCONTROLLERWith, infoheight)];
    [self.view addSubview:infoView];
    infoView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    infoView.userInteractionEnabled = YES;
    
    NSString *infoStr = @"请输入银行卡预留信息";
    float infoSize = 12;
    CGRect inforect = [df labelHeightWithContent:infoStr height:50 font:infoSize];
    UIImageView *infoImagev = [[UIImageView alloc]initWithFrame:CGRectMake(space, (infoheight-inforect.size.height)/2, inforect.size.height, inforect.size.height)];
    infoImagev.image = [UIImage imageNamed:@"tanhao.png"];
    [infoView addSubview:infoImagev];
    
    UILabel *infolabel = [UILabel initWithFrame:CGRectMake(KVIEW_X(infoImagev)+5, (infoheight-inforect.size.height)/2, KCONTROLLERWith, inforect.size.height) text:infoStr font:infoSize textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [infoView addSubview:infolabel];
    
    UIView *bankinfoView = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(infoView), KCONTROLLERWith, KWidthOfRegister*2)];
    [self.view addSubview:bankinfoView];
    bankinfoView.backgroundColor = [UIColor whiteColor];
    bankinfoView.userInteractionEnabled = YES;
    idx = 0;
    imageNameArray =@[@"手机号码",@"验证码"];
    placeholderArray=@[@"银行预留手机号",@"输入验证码"];
    for (int i = 0; i < placeholderArray.count; i++) {
        registerV = [[RegisterVIew alloc]initWithFrame:CGRectMake(0,i*KWidthOfRegister, self.view.frame.size.width, KWidthOfRegister) labeTtext:nil placeholder:placeholderArray[i]];
        registerV.userInteractionEnabled =YES;
        registerV.textField.textAlignment= NSTextAlignmentLeft;
        registerV.textField.tag = 100+i;
        registerV.tag = 1000+i;
        [registerV.textField setValue:RGBACOLOR(120, 130, 140,1) forKeyPath:@"_placeholderLabel.textColor"];
        registerV.label.textAlignment = NSTextAlignmentLeft;
        [bankinfoView addSubview:registerV];
        [self addImageWith:registerV];
        (i==0?[self addLineWith:registerV]:@"");
        registerV.textField.delegate = self;
        [registerV.textField addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
        if (i ==placeholderArray.count-1) {
            CGFloat btnHeight = 30;
            getKeyBtn = [UIButton initWithFrame:CGRectMake(KCONTROLLERWith-KBtnWidth-13, registerV.frame.origin.y+(KWidthOfRegister-btnHeight)/2, KBtnWidth-5, btnHeight) backgrandColor:nil titleColor:RGBACOLOR(89, 89, 89, 1 ) title:@"获取验证码" titleFont:12];
            [getKeyBtn addTarget:self action:@selector(getToKey) forControlEvents:UIControlEventTouchUpInside];
            
            [bankinfoView addSubview:getKeyBtn];
            [df layerWithView:getKeyBtn radius:15 width:1 color:KZhuTiRGB];
            registerV.textField.frame = CGRectMake(registerV.textField.frame.origin.x, registerV.textField.frame.origin.y, registerV.textField.frame.size.width-KBtnWidth-10, registerV.textField.frame.size.height);
        }
    }
    NSString *string = @"同意《信用卡还款服务协议》";
    float font = 12;
    CGFloat labelHeight = 30;
    CGRect buttonrect = [df labelHeightWithContent:string height:labelHeight font:font];
    CGFloat btnimagesize = buttonrect.size.height;
    CGFloat changesize = 10;
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(20, KVIEW_Y(bankinfoView)+10, buttonrect.size.width+btnimagesize+changesize, labelHeight);
    [leftButton setImage:[UIImage imageNamed:@"btn_02.png"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"btn_01.png"] forState:UIControlStateSelected];
    [leftButton setTitle:string forState:UIControlStateNormal];
    [leftButton.titleLabel setFont:[UIFont systemFontOfSize:font]];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake((labelHeight-btnimagesize)/2, 0, (labelHeight-btnimagesize)/2, buttonrect.size.width+changesize);
    leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, -btnimagesize+changesize, 0, 0);
    [leftButton setTitleColor:RGBACOLOR(100, 100, 100, 1) forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    
    
    UIButton *tijiaobutton = [UIButton buttonWithType:UIButtonTypeCustom];
    tijiaobutton.frame = CGRectMake(25, KVIEW_Y(leftButton)+30, KCONTROLLERWith-40, 50);
    [tijiaobutton addTarget:self action:@selector(tijiao) forControlEvents:UIControlEventTouchUpInside];
    [df layerWithView:tijiaobutton radius:25 width:0 color:nil];
    tijiaobutton.backgroundColor = KZhuTiRGB;
    [tijiaobutton setTitleColor:RGBACOLOR(51, 51, 51, 1) forState:UIControlStateNormal];
    [tijiaobutton setTitle:@"确认还款" forState:UIControlStateNormal];
    [self.view addSubview:tijiaobutton];
    
}
- (void)tijiao{
    NewTiXianFinishViewController *vc = [[NewTiXianFinishViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark --重新获取验证码
- (void)getTheCode{
    [self.view endEditing:YES];
    
    LoadData *request = [[LoadData alloc]init];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
    [dict setObject:@"credit_card" forKey:@"do"];
//    NSLog(@"%@",phonenumber);
//    [dict setObject:phonenumber forKey:@"mobile"];
    [dict setObject:KTokenString forKey:@"token"];
    
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"User/smsverified") withDict:dict superView:self.view isAddtag:NO Block:^(id result) {
        
        [self gettime];
        
        NSLog(@"*********************%@",result);
        if (result == nil ) {
            [df showAlertWithTitle:@"获取验证码失败" message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
            return ;
        }
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"result"]];
        if ([isreturn isEqualToString:@"1"]) {
            [df showAlertWithTitle:[result objectForKey:@"msg"] message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        }else{
            [df showAlertWithTitle:[result objectForKey:@"msg"] message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        }
        [getKeyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }];
    
    
}
- (void)getToKey{
    
    [self getTheCode];
    
}



#pragma mark --添加灰线条
-(void)addImageWith:(UIView *)fatherview{
    
    UILabel *lable =[[UILabel alloc] initWithFrame:CGRectMake(20,  0, 77, KWidthOfRegister-1.5)];
    [lable setText:imageNameArray[idx++]];
    lable.textAlignment=YES;
    lable.textAlignment = NSTextAlignmentLeft;
    lable.backgroundColor =[UIColor whiteColor];
    lable.font =[UIFont fontWithName:@"Arial" size:15.0];
    [fatherview addSubview:lable];
    
    
}
- (void)addLineWith:(UIView *)fatherview{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, fatherview.frame.size.height-1.5, KCONTROLLERWith-40, 1.5)];
    view.backgroundColor = RGBACOLOR(226, 227, 228,1);
    [fatherview addSubview:view];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder] ;
    return YES;
}
- (void)removeKeyBoard{
    [self.view endEditing:YES];
}
#pragma mark --定时器
- (void)gettime{
    [_timer2 invalidate];
    [_timer1 invalidate];
    
    count = 0;
    
    NSTimer* timer=[NSTimer scheduledTimerWithTimeInterval:60
                                                    target:self
                                                  selector:@selector(showRepeatButton)
                                                  userInfo:nil
                                                   repeats:YES];
    
    NSTimer* timer2=[NSTimer scheduledTimerWithTimeInterval:1
                                                     target:self
                                                   selector:@selector(updateTime)
                                                   userInfo:nil
                                                    repeats:YES];
    _timer1=timer;
    _timer2=timer2;
    
}
-(void)updateTime{
    count++;
    if (count>60)
    {
        [_timer2 invalidate];
        return;
    }
    NSString *timeStr=[NSString stringWithFormat:@"%lus重新获取",(long)60-count];
    [getKeyBtn setTitle:timeStr forState:UIControlStateNormal];
    if(count == 60){
        [getKeyBtn setTitle:@"重新获取" forState:UIControlStateNormal];
    }
    
}
-(void)showRepeatButton{
    
    [_timer1 invalidate];
    return;
}

#pragma mark 监听textfield 的变化
- (void)valueChanged:(UITextField *)textfield{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
